//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "DouglasScheme.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Experimental/Finitedifferences/FdmLinearOpComposite.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CDouglasScheme::CDouglasScheme (boost::shared_ptr<QuantLib::DouglasScheme>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDouglasScheme = NULL;
#endif
	_ppDouglasScheme = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CDouglasScheme::CDouglasScheme (QuantLib::DouglasScheme& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phDouglasScheme = NULL;
#endif
	_ppDouglasScheme = new boost::shared_ptr<QuantLib::DouglasScheme> (&childNative);
    
    _DouglasSchemeOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CDouglasScheme::CDouglasScheme (CDouglasScheme^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDouglasScheme = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDouglasScheme = new boost::shared_ptr<QuantLib::DouglasScheme> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CDouglasScheme::CDouglasScheme (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDouglasScheme = NULL;
#endif
	if (!t->IsSubclassOf(CDouglasScheme::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CDouglasScheme::CDouglasScheme (QuantLib::Handle<QuantLib::DouglasScheme>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDouglasScheme = &childNative;
	_ppDouglasScheme = &static_cast<boost::shared_ptr<QuantLib::DouglasScheme>>(childNative.currentLink());
    
    _DouglasSchemeOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CDouglasScheme::CDouglasScheme (QuantLib::Handle<QuantLib::DouglasScheme> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phDouglasScheme = &childNative;
	_ppDouglasScheme = &static_cast<boost::shared_ptr<QuantLib::DouglasScheme>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CDouglasScheme::CDouglasScheme (QuantLib::DouglasScheme childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phDouglasScheme = NULL;
#endif
	_ppDouglasScheme = new boost::shared_ptr<QuantLib::DouglasScheme> (new QuantLib::DouglasScheme (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CDouglasScheme::~CDouglasScheme ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppDouglasScheme != NULL)
    {
	    delete _ppDouglasScheme;
        _ppDouglasScheme = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CDouglasScheme::!CDouglasScheme ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppDouglasScheme != NULL)
    {
	    delete _ppDouglasScheme;
    }
}
QuantLib::DouglasScheme& Cephei::QL::Experimental::Finitedifferences::CDouglasScheme::GetReference ()
{
    if (_ppDouglasScheme == NULL) throw gcnew NativeNullException ();
	return **_ppDouglasScheme;
}
boost::shared_ptr<QuantLib::DouglasScheme>& Cephei::QL::Experimental::Finitedifferences::CDouglasScheme::GetShared ()
{
    if (_ppDouglasScheme == NULL) throw gcnew NativeNullException ();
	return *_ppDouglasScheme;
}
QuantLib::DouglasScheme* Cephei::QL::Experimental::Finitedifferences::CDouglasScheme::GetPointer ()
{
    if (_ppDouglasScheme == NULL) throw gcnew NativeNullException ();
	return &**_ppDouglasScheme;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DouglasScheme>& Cephei::QL::Experimental::Finitedifferences::CDouglasScheme::GetHandle ()
{
	if (_phDouglasScheme == NULL)
	{
		_phDouglasScheme = new Handle<QuantLib::DouglasScheme> (*_ppDouglasScheme);
	}
	return *_phDouglasScheme;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CDouglasScheme::HasNative () 
{
	return (_ppDouglasScheme != NULL);
}

Cephei::QL::Experimental::Finitedifferences::IDouglasScheme^ Cephei::QL::Experimental::Finitedifferences::CDouglasScheme::SetStep (Double dt)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt);
    	(*_ppDouglasScheme)->setStep ( _dt );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

